package org.example;

import Service.Impl.UserServiceImpl;
import Service.UserService;
import init.DishesInit;
import init.UserInit;
import model.Dishes;
import model.User;

import java.io.*;
import java.util.Scanner;

import static init.DishesInit.dishes;
import static init.UserInit.users;



public class Main {
    static String userName = null;
    static String password = null;
    public static void main(String[] args) throws IOException {
        new UserInit();
        new DishesInit();
        int choice = 0;
        while (choice != -1) {

            System.out.println("------------欢迎来到饿了么平台------------");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.查看所有菜品信息");
            System.out.println("4.下单");
            System.out.println("5.欢迎您再次点餐");
            System.out.println("请输入你的选择");
            Scanner sc = new Scanner(System.in);
            choice = sc.nextInt();
            switch (choice) {
                case 1:
                    Scanner scanner = new Scanner(System.in);
                    System.out.println("1.您好,请登录");     //登录功能
                    //调用service中的登录功能
                    System.out.println("请输入用户名：");
                    userName = scanner.nextLine();

                    System.out.println("请输入密码：");
                    password = scanner.nextLine();

                    UserService userService = new UserServiceImpl();
                    boolean flag = userService.userLogin(userName, password);
                    if (flag) {
                        System.out.println("登录成功!");
                    } else {
                        System.out.println("用户名或密码错误!");
                    }
                    break;
                case 2:
                    Scanner scanner1 = new Scanner(System.in);
                    System.out.println("2.请注册");  //注册功能
                    System.out.println("请输入用户名");
                    String userNameRegister = scanner1.nextLine();

                    System.out.println("请输入密码");
                    String passwordRegister = scanner1.nextLine();

                    System.out.println("请输入年龄");
                    int ageRegister = scanner1.nextInt();
                    scanner1.nextLine();
                    System.out.println("请输入电话号码");
                    String phoneRegister = scanner1.nextLine();

                    System.out.println("请充值");
                    double priceRegister = scanner1.nextDouble();

                    UserService userService1 = new UserServiceImpl();
                    boolean register = userService1.userRegister(userNameRegister, passwordRegister, ageRegister, phoneRegister, priceRegister);
                    if (register) {
                        System.out.println("注册成功");
                    } else {
                        System.out.println("注册失败");
                    }
                    break;
                case 3:
                    if(userloginCheck()) {
                        System.out.println("3.查看所有菜品信息");   //查询功能

                        for (Dishes dishe : dishes) {
                            System.out.println("餐品ID："+dishe.getDid()+" "+"餐品名称："+dishe.getDname()+" "+"餐品介绍："+dishe.getDetails()+" "+"餐品销量："+dishe.getCount()+" "+"餐品份数："+dishe.getNum()+" "+"餐品价格："+dishe.getPrice());
                        }
                        break;
                    }
                    System.out.println("登录信息错误，请重新登录");
                    break;
                case 4:
                    if (userloginCheck()) {
                        Scanner scanner2 = new Scanner(System.in);
                        System.out.println("4.下单");             //先查询再修改
                        System.out.println("请选择要购买的菜品ID：");

                        int dishId = scanner2.nextInt();
                        for (Dishes dishes1 : dishes) {
                            if (dishes1.getDid() == dishId) {
                                int num = dishes1.getNum() - 1;
                                dishes1.setNum(num);
                                //changeDishesNum(dishes1.getDid());
                                for (User user : users) {
                                    if (userName.equals(user.getUsername()) && password.equals(user.getPassword())) {
                                        //找到这个用户
                                        if (user.getMoney() < dishes1.getPrice()) {
                                            System.out.println("钱不够捏");
                                            break;
                                        } else {
                                            user.setMoney(user.getMoney() - dishes1.getPrice());
                                            System.out.println("余额：" + user.getMoney());
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    }
                    break;
                case 5:
                    System.out.println("5.欢迎您再次点餐");    //退出
                    break;
            }

        }
    }
/*
    private static void changeDishesNum(long did) throws IOException{
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("dishes.txt"));
        BufferedReader bufferedReader  = new BufferedReader(new FileReader("dishes.txt"));
        for(Dishes dishes1 :dishes){
            String data = bufferedReader.readLine();
            String[] split = data.split(",");
            String id = split[0];
            String num = split[4];
            if (Long.toString(did).equals(id)){
                Integer int_num = Integer.valueOf(num);
                int_num--;

            }
        }
    }
*/
    private static boolean userloginCheck() {
        for(User user:users){
            if (userName.equals(user.getUsername())&& password.equals(user.getPassword())){
                return true;
            }
        }
        return false;
    }
}